package org.iartisan.maven.plugin.jdbc;

import org.iartisan.maven.plugin.exception.DBParseException;
import org.iartisan.maven.plugin.jdbc.bean.TableColumn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * mysql相关表处理
 *
 * @author King
 * @since 2017/8/30
 */
public class MySQLTableHandler implements TableHandler {

    private Connection connection;

    public MySQLTableHandler(Connection connection) {
        this.connection = connection;
    }

    public static final String sql_show_tables = "show tables";//查询数据库中所有表

    public static final String sql_show_fields = "show full fields from %s";//显示表中所有列

    public static final String FIELD = "FIELD";//列名
    public static final String TYPE = "TYPE";//类型
    public static final String COMMENT = "COMMENT";//备注
    public static final String KEY = "KEY";//列key

    public List<String> getTables(String[] tableNames) throws SQLException, DBParseException {
        List<String> tables = new ArrayList<String>();
        PreparedStatement pstate = connection.prepareStatement(sql_show_tables);
        ResultSet results = pstate.executeQuery();
        while (results.next()) {
            tables.add(results.getString(1));
        }
        if (null == tableNames || tableNames.length == 0) {
            return tables;
        }
        List<String> custTables = Arrays.asList(tableNames);
        //Collections.
        List<String> notExistTables = new ArrayList<String>();
        for (String tb : custTables) {
            if (!tables.contains(tb)) {
                notExistTables.add(tb);
            }
        }
        if (notExistTables.size() == 0) {
            return custTables;
        }
        throw new DBParseException("表:" + notExistTables.toString() + "不存在");
    }


    public List<TableColumn> getTableFields(String tableName) throws SQLException {
        String sql = String.format(sql_show_fields, tableName);
        ResultSet results = connection.prepareStatement(sql).executeQuery();
        List<TableColumn> tableColumns = new ArrayList<TableColumn>();
        while (results.next()) {
            String field = results.getString(FIELD);//列名
            String type = results.getString(TYPE);//类型
            String comment = results.getString(COMMENT);//备注
            String key = results.getString(KEY);//是否主键
            TableColumn column = new TableColumn();
            //解析field
            column.setColumn(field);
            column.setProperty(DBUtil.getProperty(field));
            column.setComment(comment);
            column.setJdbcType(DBUtil.getJdbcType(type));//转换jdbcType
            column.setJavaType(DBUtil.getJavaType(column.getJdbcType()));
            //column.setImportPackage(DBUtil.getImportPackage(column.getJavaType()));
            //判断是否为主键
            if ("PRI".equals(key)) {
                column.setPrimaryKey(true);
            }
            tableColumns.add(column);
        }
        return tableColumns;
    }

}
